import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_redux/flutter_redux.dart';
import 'package:my_flutter_github/common/DataResult.dart';
import 'package:my_flutter_github/common/dao/UserDao.dart';
import 'package:my_flutter_github/model/User.dart';
import 'package:my_flutter_github/model/UserOrg.dart';
import 'package:my_flutter_github/page/BasePersonState.dart';
import 'package:my_flutter_github/widgets/CommonOptionWidget.dart';
import 'package:my_flutter_github/redux/MyState.dart';

///个人信息界面
class PersonInfoPage extends StatefulWidget {
  static const sName = "/person";

  final String userName;

  const PersonInfoPage({Key key, this.userName}) : super(key: key);

  @override
  _PersonInfoPageState createState() => _PersonInfoPageState(userName);
}

class _PersonInfoPageState extends BasePersonState<PersonInfoPage> {

  final String userName;

  String beStaredCount = "---";

  bool focusStatus = false;

  String focus = "";

  User userInfo = User.empty();

  final List<UserOrg> orgList = new List();

  final OptionControl titleOptionControl = new OptionControl();

  _PersonInfoPageState(this.userName);

  _handleUserInfo(DataResult result) {
    if(isShow) {
      setState(() {
          userInfo = result.data;
          titleOptionControl.url = result.data.html_url;
      });
    }
  }

  @override
  Future<Null> onRefresh() async {
    if (isLoading) {
      return null;
    }
    isLoading = true;
    pageIndex = 1;
    var userResult = await UserDao.getUserInfo(userName, needDb: true);
    if (userResult != null && userResult.result) {
      _handleUserInfo(userResult);
      if (userResult.next != null) {
        userResult.next.then((resNext) {
          _handleUserInfo(resNext);
        });
      }
    } else {
      return null;
    }
    var res = await _getDataLogic();
    handleRefreshResult(res);
    handleLoadMoreResult(res);
    if (res.next != null) {
      var resNext = await res.next;
      handleRefreshResult(resNext);
      handleLoadMoreResult(resNext);
    }
    isLoading = false;
    _getFocusStatus();
//    ReposDao.getUserRepository100StatusDao(_getUserName()).then((res) {
//      if (res != null && res.result) {
//        if (isShow) {
//          setState(() {
//            beStaredCount = res.data.toString();
//          });
//        }
//      }
//    });
    return null;
  }

  ///用户关注状态
  _getFocusStatus() async {
    var focusRes = await UserDao.checkFollowDao(userName);
    if (isShow) {
      setState(() {
        focus = (focusRes != null && focusRes.result) ? "已关注" : "未关注";
        focusStatus = (focusRes != null && focusRes.result);
      });
    }
  }

  _getUserName() {
    if (userInfo == null) {
      return new User.empty();
    }
    return userInfo.login;
  }

  _getDataLogic() async {
    if (userInfo.type == "Organization") {
      return await UserDao.getMemberDao(_getUserName(), pageIndex);
    }
    getUserOrg(_getUserName());
//    return await EventDao.getEventDao(_getUserName(), page: pageIndex, needDb: pageIndex <= 1);
    return null;
  }

  @override
  bool get wantKeepAlive => true;

  @override
  requestLoadMore() async {
    return await _getDataLogic();
  }

  @override
  bool get autoRefresh => true;

  @override
  bool get needHeader => true;


  @override
  Widget build(BuildContext context) {
    return new StoreBuilder<MyState>(builder: (context, store) {

    });
  }
}
